﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Forging2.Common
{
    class UpdateWorkpiece
    {
        public WorkingPiece Workpiece { get; private set; }
        public int State { get; set; }
        public string OperateCode { get; set; }
        public int  InOut { get; set; }
        public bool IsFinshed { get; private set; } = false;
        public UpdateWorkpiece()
        {
            
        }
        //public UpdateWorkpiece(WorkingPiece workpiece,int operatetype,string operatecode,int inout)
        //{
        //    Workpiece = workpiece;
        //    State = operatetype;
        //    OperateCode = operatecode;
        //    InOut = inout;
        //}

        /// <summary>
        /// 后台数据操作
        /// </summary>
        /// <returns></returns>
        private Task<int> UpdateWorkpieceRecord()
        {
            System.Data.SqlClient.SqlParameter p_state = new System.Data.SqlClient.SqlParameter("@state", System.Data.SqlDbType.Int);
            p_state.Value = State;
            System.Data.SqlClient.SqlParameter p_gid = new System.Data.SqlClient.SqlParameter("@datagid", System.Data.SqlDbType.VarChar);
            p_gid.Value = Workpiece.GID;
            System.Data.SqlClient.SqlParameter p_code = new System.Data.SqlClient.SqlParameter("@operatecode", System.Data.SqlDbType.VarChar);
            p_code.Value = OperateCode;
            System.Data.SqlClient.SqlParameter p_inout = new System.Data.SqlClient.SqlParameter("@inout", System.Data.SqlDbType.Int);
            p_inout.Value = 0;
            System.Data.SqlClient.SqlParameter p_return = new System.Data.SqlClient.SqlParameter("@return", System.Data.SqlDbType.Int);
            p_return.Value = 0;
            p_return.Direction = System.Data.ParameterDirection.Output;

            Const.ZH_IMESDbContext.Database.ExecuteSqlCommand("exec [Forging_Operations]  @datagid, @operatecode, @inout, @state, @return OUTPUT",  p_gid, p_code,p_inout,p_state, p_return);
            Const.ZH_IMESDbContext.SaveChangesAsync();
            IsFinshed = true;
            if (DBNull.Value==p_return.Value)
            {
                p_return.Value = 0;
            }
            return Task.FromResult((int)p_return.Value);
        }
        
        /// <summary>
        /// 更新后台数据
        /// </summary>
        /// <param name="workpiece">工件信息</param>
        /// <param name="operatecode">操作者</param>
        /// <param name="state">操作类型 0-无，1-开工，2-暂停，3-完工，10-其他</param>
        /// <param name="inout">走炉状态</param>
        /// <returns></returns>
        public Task<int> UpdateWorkpieceRecord(WorkingPiece workpiece,  int state, int inout)
        {
            Workpiece = workpiece;
            State = state;
            OperateCode = workpiece.Operater;
            InOut = inout;
            return UpdateWorkpieceRecord();
            
        }
    }
}
